package com.gsoe.judge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.gsoe.base.exception.ProblemException;
import com.gsoe.base.model.PageResult;
import com.gsoe.base.model.Result;
import com.gsoe.base.model.User;
import com.gsoe.base.utils.UserUtil;
import com.gsoe.dto.CheckProblemAnswerDTO;
import com.gsoe.dto.ProblemRecordDTO;
import com.gsoe.judge.client.ProblemClient;
import com.gsoe.judge.mapper.JudgeMapper;

import com.gsoe.judge.service.JudgeService;
import com.gsoe.po.Judge;
import com.gsoe.vo.JudgeVO;
import com.gsoe.vo.ProblemRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 用户刷题记录 服务实现类
 * </p>
 *
 * @author xiaoma
 * @since 2024-11-04
 */
@Service
public class JudgeServiceImpl extends ServiceImpl<JudgeMapper, Judge> implements JudgeService {
    @Autowired
    private ProblemClient problemClient;

    @Autowired
    private JudgeMapper judgeMapper;

    @Override
    public JudgeVO submitAnswer(CheckProblemAnswerDTO checkProblemAnswerDTO) {
        Result<JudgeVO> judgeResult = problemClient.checkProblemAnswer(checkProblemAnswerDTO);
        if (judgeResult.getCode() != 200) throw new ProblemException(judgeResult.getMsg());
        Judge judge = new Judge();
        judge.setProblemId(checkProblemAnswerDTO.getProblemId());
        judge.setInput(checkProblemAnswerDTO.getAnswer());
        judge.setResult(judgeResult.getData().getIsRight());
        User loginUser = UserUtil.getLoginUser();
        judge.setUserId(loginUser.getId());
        judge.setUserName(loginUser.getUserName());
        judge.setProblemDescribe(judgeResult.getData().getProblemDescribe());
        judge.setCreateTime(LocalDateTime.now());
        save(judge);
        return judgeResult.getData();
    }

    @Override
    public PageResult<ProblemRecordVO> getProblemRecord(ProblemRecordDTO problemRecordDTO) {
        if (problemRecordDTO.getUserId() == null) {
            User loginUser = UserUtil.getLoginUser();
            problemRecordDTO.setUserId(loginUser.getId());
        }
        Page<Judge> page = new Page<>(problemRecordDTO.getPageNo(), problemRecordDTO.getPageSize());


        page = page(page, new QueryWrapper<Judge>()
                .eq("user_id", problemRecordDTO.getUserId())
                .eq(problemRecordDTO.getProblemId() != null, "problem_id", problemRecordDTO.getProblemId())
                .like(problemRecordDTO.getProblemDescribe() != null, "problem_describe", problemRecordDTO.getProblemDescribe())
                .eq(problemRecordDTO.getIsRight() != null, "result", problemRecordDTO.getIsRight())
                .orderByDesc("create_time")
        );
        return PageResult.of(page, judge -> {
            ProblemRecordVO problemRecordVO = new ProblemRecordVO();
            BeanUtils.copyProperties(judge, problemRecordVO);
            return problemRecordVO;
        });
    }

    @Override
    public List<Judge> getProblemNumber(Long userId) {
        List<Judge> judgesList = judgeMapper.getSolveNumber(userId);
        return judgesList;
    }
}
